/*----------------------------------------------------------------------
  - File name      : STC8Ax_COMP.c
  - Author         : Quark Team
  - Update date    : 2020-08-24                   
  -	Copyright      : Gevico Electronic studio   
  - Module comments: COMP module.
-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
|                               INCLUDES                                |
-----------------------------------------------------------------------*/
#include "STC8Ax_COMP.h"
/*-----------------------------------------------------------------------
|                                 DATA                                  |
-----------------------------------------------------------------------*/
/*--------------------------------------------------------
| @Description: COMP PEPin select define function        |
--------------------------------------------------------*/

#define  COMP_PEINPUT_CFG(PEPin)  { \
if(PEPin == COMP_PE_P37) {CMPCR1 &= 0xF7;}\
else {CMPCR1 |= 0x08; \
ADC_CONTR = (ADC_CONTR & 0xF0)|(PEPin);  }}

/*--------------------------------------------------------
| @Description: COMP NEPin select define function        |
--------------------------------------------------------*/

#define  COMP_NEINPUT_CFG(NEPin) CMPCR1 = (CMPCR1 & 0xFB) | ((NEPin & 0xF0) >> 2)

/*-----------------------------------------------------------------------
|                               FUNCTION                                |
-----------------------------------------------------------------------*/

/*--------------------------------------------------------
| @Description: COMP init function                       |
| @param      : const COMP_InitType *COMPx               |
| @return     : FSCSTATE : Success / Fail                |
--------------------------------------------------------*/

FSCSTATE COMP_Init(const COMP_InitType *COMPx)
{
  if(COMPx -> DC_FTW > 0x3F) return FSC_FAIL;
  COMP_PEINPUT_CFG(COMPx -> PEPin);
  COMP_NEINPUT_CFG(COMPx -> NEPin);
  CMPCR2 = (CMPCR2 & 0xBF) | (COMPx -> AC_FTW << 6);
  CMPCR2 = (CMPCR2 & 0xC0) | (COMPx -> DC_FTW);
  CMPCR2 = (CMPCR2 & 0x7F) | (COMPx -> DIRPut << 7);
  CMPCR1 = (CMPCR1 & 0xFD) | (COMPx -> Output << 1);
	CMPCR1 = (CMPCR1 & 0x7F) | (COMPx -> Run << 7);
  return FSC_SUCCESS;
}

/*-----------------------------------------------------------------------
|          END OF FLIE        (C) COPYRIGHT Gevico Electronics          | 
-----------------------------------------------------------------------*/



